<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'public';

    protected $system = [];

    public function _initialize()
    {
        /**自定义配置**/
        $system = model('app\common\model\SystemModel')->get(1);
        $this->system = $system;
        $this->assign('system', $system);

        /**Banner**/
        $actionname = strtolower($this->request->action());
        $position = $actionname;
        if($actionname == 'newsDetail') {
            $position = 'news';
        }
        if($actionname == 'index') {
            $banner = model('app\common\model\BannerModel')
                ->where(['position' => $position,'status' => '1'])
                ->order('weigh','asc')
                ->select();
        } else {
            $banner = model('app\common\model\BannerModel')
                ->where(['position' => $position,'status' => '1'])
                ->order('weigh','asc')
                ->find();
        }
        $this->assign('position', $position);
        $this->assign('banner', $banner);

        /**右侧外部跳转链接**/
        $third_qrcode = model('app\common\model\ThirdQrcodeModel')
//            ->field('id,name,image,qrcode_image')
            ->where('status','=','1')
            ->order('weigh','asc')
            ->select();
        $this->assign('third_qrcode', $third_qrcode);

        /**导航栏-形象广告**/
        $advertising_page_limit = 18;
        $advertising = model('app\admin\model\Advertising')
            ->field('id,name,image')
            ->where('status','=','1')
            ->order('weigh','asc')
//            ->limit(4)
            ->select();
        $advertising_middle = array_chunk($advertising, $advertising_page_limit);
        foreach ($advertising_middle as $key => $val) {
            $remainder = count($val)%$advertising_page_limit;
            if($remainder != 0) {
                $diff = $advertising_page_limit - count($val);
                for ($i=0;$i<$diff;$i++) {
                    $val[] = [];
                }
                $advertising_middle[$key] = $val;
            }
        }
        $new_advertising_middle = [];
        foreach ($advertising_middle as $val) {
            foreach ($val as $value) {
                $new_advertising_middle[] = $value;
            }
        }
        $advertising = $new_advertising_middle;
        $this->assign('advertising', $advertising);
        $advertising_nav_right = model('app\admin\model\Advertising')
            ->field('id,name,image')
            ->where('status','=','1')
            ->where('is_nav_right','=','1')
            ->order('weigh','asc')
            ->limit(2)
            ->select();
        $this->assign('advertising_nav_right', $advertising_nav_right);

        /**导航栏-最新单品**/
        $product = model('app\admin\model\NewProduct')
            ->field('id,name,image')
            ->where('status','=','1')
            ->order('weigh','asc')
            ->limit(4)
            ->select();
        $this->assign('product', $product);
        $product_nav_right = model('app\admin\model\NewProduct')
            ->field('id,name,image')
            ->where('status','=','1')
            ->where('is_nav_right','=','1')
            ->order('weigh','asc')
            ->limit(2)
            ->select();
        $this->assign('product_nav_right', $product_nav_right);

        /**关于我们配置**/
        $about_config = model('app\admin\model\AboutConfig')->get(1);
        $this->about_config = $about_config;
        $this->assign('about_config', $about_config);

        /**终端店铺配置**/
        $shop_config = model('app\admin\model\shop\Config')->get(1);
        $shop_config->images = full_url($shop_config->images,1);
        $this->shop_config = $shop_config;
        $this->assign('shop_config', $shop_config);

        /**终端店铺配置**/
        $develop_config = model('app\admin\model\develop\Config')->get(1);
        $develop_config->images5 = full_url($develop_config->images5,1);
        $this->develop_config = $develop_config;
        $this->assign('develop_config', $develop_config);

        /**资讯配置**/
        $news_config = model('app\admin\model\news\Config')->get(1);
        $this->news_config = $news_config;
        $this->assign('news_config', $news_config);

        parent::_initialize(); // TODO: Change the autogenerated stub
    }

    public function index()
    {
        $season_show = model('app\common\model\seasonshow\SeasonShowModel')
            ->field('id,name,image,video')
            ->where('status','=','1')
            ->limit(2)
            ->order('weigh','asc')
            ->select();
        $this->assign('season_show', $season_show);

        /**形象广告**/
        $advertising_index = model('app\admin\model\Advertising')
            ->field('id,name,image,images2')
            ->where('status','=','1')
            ->order('weigh','desc')
            ->limit(2)
            ->select();
        foreach ($advertising_index as $row) {
            $row->images2 = full_url($row->images2,true);
        }
        $this->assign('advertising_index', $advertising_index);

        /**最新单品**/
        $new_product_id_arr = model('app\admin\model\NewProduct')
            ->where('status','=','1')
            ->order('weigh','desc')
            ->column('id');
        $product_images = model('app\admin\model\NewProductDetail')
            ->field('id,new_product_id,name,image')
            ->where('status','=','1')
            ->whereIn('new_product_id',$new_product_id_arr)
            ->order('weigh','desc')
            ->select();
        $this->assign('product_images', $product_images);

        return $this->view->fetch();
    }

    public function show()
    {
        $id = $this->request->request('id');
        $this->assign('id', $id);

        $year_id = $this->request->request('year');
        if(!$id) $this->error('数据错误');
        $data = model('app\common\model\seasonshow\SeasonShowModel')->get($id);
        if(!$data) $this->error('数据错误');
        $data->images4 = full_url($data->images4,1);
        $this->assign('data', $data);

        $product_where = [
            'status' => '1',
            'season_show_id' => $id,
        ];
        if(!$year_id) {
            $year_id = model('app\common\model\seasonshow\YearModel')
                ->where(['status' => '1'])
                ->order('weigh','asc')
                ->value('id');
        }
        $product_where['year_id'] = $year_id;
        $this->assign('year_id', $year_id);

        $product = model('app\common\model\seasonshow\ProductModel')
            ->where($product_where)
            ->order('weigh','asc')
            ->select();
        $this->assign('product', $product);

        $year = model('app\common\model\seasonshow\YearModel')
            ->where(['status' => '1'])
            ->order('weigh','asc')
            ->select();
        $this->assign('year', $year);

//        $system = $this->system;
//        $system['keywords'] = '季节展示';
//        $system['description'] = '季节展示';
//        $this->assign('system', $system);
        return $this->view->fetch();
    }


    public function advertising()
    {
        $id = $this->request->request('id');
        $this->assign('id', $id);
        if(!$id) $this->error('数据错误');
        $data = model('app\admin\model\Advertising')->get($id);
        if(!$data) $this->error('数据错误');
        $data->images2 = full_url($data->images2,1);
        if($data->images2){
            $images2_1 = [];
            $images2_2 = [];
            foreach ($data->images2 as $key => $row){
                if($key%2 == 0){
                    $images2_2[] = $row;
                }else{
                    $images2_1[] = $row;
                }
            }
            $data->images2 = [$images2_1,$images2_2];
        }
        $data->images4 = full_url($data->images4,1);
        $data->image5 = full_url($data->image5);
        $this->assign('data', $data);

        return $this->view->fetch();
    }

    public function product()
    {
        $id = $this->request->request('id');
        $this->assign('id', $id);
        if(!$id) $this->error('数据错误');
//        $page = $this->request->request('page',1);
//        $limit = $this->request->request('limit',8);
//        $data = model('app\admin\model\NewProductDetail')
//            ->field('id,new_product_id,name,image,images')
//            ->where(['status' => '1','new_product_id' => $id])
//            ->order('weigh','asc')
//            ->paginate($limit, false, ['page' => $page]);
//        if(!$data) $this->error('数据错误');
//        foreach ($data as $row) {
//            $row->images = full_url($row->images,1);
//        }
//        $this->assign('data', $data);
        return $this->view->fetch();
    }

    public function about()
    {
        return $this->view->fetch();
    }

    public function develop()
    {
        $develop = model('app\admin\model\develop\Develop')
            ->where(['status' => '1'])
            ->order('weigh','asc')
            ->select();
        $this->assign('develop', $develop);
        return $this->view->fetch();
    }

    public function shop()
    {
        $province_name = $this->request->request('province_name');
        $this->assign('province_name', $province_name);
        $city_name = $this->request->request('city_name');
        $this->assign('city_name', $city_name);
        $shop_name = $this->request->request('shop_name');
        $this->assign('shop_name', $shop_name);

        if(!$province_name) {
            $ip = request()->ip();
            $address = getBaiduIpAddress($ip);
            if($address && isset($address['content']['address_detail']['province'])) {
                $province_name = $address['content']['address_detail']['province'];
                $this->assign('province_name', $province_name);
            }
        }

        $where = [
            'status' => '1',
        ];
        if(!empty($province_name) && $province_name != '全部') {
            $where['province'] = $province_name;
        }
        if(!empty($city_name)) {
            $where['city'] = $city_name;
        }
        if(!empty($shop_name)) {
            $where['name'] = $shop_name;
        }
        $exp = new \think\db\Expression('convert(name using gbk) asc');
        $shop = model('app\admin\model\shop\Shop')
            ->where($where)
            ->whereNotNull('lat')
            ->orderRaw($exp)
            ->order('weigh','asc')
            ->select();
        $this->assign('shop', $shop);

        $map_data = [];
        foreach ($shop as $row) {
            $map_data[] = [
                'title' => $row->name,
                'content' => "{$row->detail}<br/>{$row->tel}",
                'lng' => $row->lng,
                'lat' => $row->lat,
                'point' => "{$row->lng}|{$row->lat}",
                'isOpen' => "1",
                'icon' => ['w'=>23, 'h'=>25, 'l'=>46, 't'=>21, 'x'=>9, 'lb'=>12],
            ];
        }
//        $map_data = array_reverse($map_data);
        $this->assign('map_data', json_encode($map_data));

        $exp = new \think\db\Expression('convert(province using gbk) asc');
        $province = model('app\admin\model\shop\Shop')
            ->where(['status' => '1'])
            ->whereNotNull('lat')
            ->orderRaw($exp)
            ->group('province')
            ->column('province');
        $this->assign('province', $province);
        $city = [];
        if(!empty($province_name)) {
            $city = model('app\admin\model\shop\Shop')
                ->where(['status' => '1','province' => $province_name])
                ->whereNotNull('lat')
                ->orderRaw($exp)
                ->group('city')
                ->column('city');
        }
        $this->assign('city', $city);

        $this->auth->login('admin',123456);
        $token = $this->auth->getUserinfo()['token'];
        $this->assign('token',$token);

        return $this->view->fetch();
    }

    public function join()
    {
        $join = model('app\admin\model\join\Join')
            ->where(['status' => '1'])
            ->order('weigh','asc')
            ->select();
        $this->assign('join', $join);
        $this->auth->login('admin',123456);
        $token = $this->auth->getUserinfo()['token'];
        $this->assign('token',$token);
        return $this->view->fetch();
    }

    public function contact()
    {
        $thirdqrcode = model('app\admin\model\Thirdqrcode')
            ->where(['status' => '1','qrcode_image' => ['<>','']])
            ->whereNotNull('qrcode_image')
            ->order('weigh','asc')
            ->select();
        $this->assign('thirdqrcode', $thirdqrcode);
        $this->auth->login('admin',123456);
        $token = $this->auth->getUserinfo()['token'];
        $this->assign('token',$token);
        return $this->view->fetch();
    }

    public function news()
    {
        $search = $this->request->request('search');
        $page = $this->request->request('page',1);
        $limit = $this->request->request('limit',5);
        $where = [
            'status' => '1',
            'mold' => 'news'
        ];
        if(!empty($search)) {
            $where['title'] = ['like',"%{$search}%"];
        }
        $news = model('app\admin\model\news\News')
            ->where($where)
            ->order('weigh','asc')
            ->paginate($limit, false, ['page' => $page]);
        $this->assign('news', $news);
        return $this->view->fetch();
    }

    public function brand()
    {
        $page = $this->request->request('page',1);
        $limit = $this->request->request('limit',5);
        $brand = model('app\admin\model\news\News')
            ->where(['status' => '1','mold' => 'brand'])
            ->order('weigh','asc')
            ->paginate($limit, false, ['page' => $page]);
        $this->assign('brand', $brand);
        return $this->view->fetch();
    }

    public function newsdetail()
    {
        $id = $this->request->request('id');
        $this->assign('id', $id);
        if(!$id) $this->error('数据错误');
        $detail = model('app\admin\model\news\News')->get($id);
        $this->assign('detail', $detail);
        $other = model('app\admin\model\news\News')
            ->where(['status' => '1','mold' => $detail->mold,'id' => ['<>',$id]])
            ->order('weigh','asc')
            ->select();
        $this->assign('other', $other);
        return $this->view->fetch();
    }

}
